package com.zzw.system.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzw.model.system.SysRole;
import com.zzw.model.system.SysUserRole;
import com.zzw.vo.system.AssginRoleVo;
import com.zzw.vo.system.SysRoleQueryVo;
import com.zzw.system.mapper.SysRoleMapper;
import com.zzw.system.mapper.SysUserRoleMapper;
import com.zzw.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Override
    public IPage<SysRole> selectPage(Page<SysRole> pageParam, SysRoleQueryVo roleQueryVo) {
        return baseMapper.selectPage(pageParam,roleQueryVo);
    }

    @Override
    public Map<String, Object> getRolesById(Long userId) {

//        获取所有角色
        List<SysRole> roles = baseMapper.selectList(null);

//        根据用户id查询
        QueryWrapper<SysUserRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
//        获取用户已分配的角色
        List<SysUserRole> userRoles = sysUserRoleMapper.selectList(queryWrapper);
//        获取用户已分配的角色id
        List<Long> userRoleIds=new ArrayList<>();
        for( SysUserRole userRole:userRoles){
            userRoleIds.add(userRole.getRoleId());
        }

//        创建返回的Map
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("allRoles",roles);
        returnMap.put("userRoleIds",userRoleIds);
        return returnMap;
    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
//        根据用户id删除原来分配的角色
        QueryWrapper<SysUserRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",assginRoleVo.getUserId());
        sysUserRoleMapper.delete(queryWrapper);
//        获取所分配的角色id
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for (Long roleId:roleIdList){
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setUserId(assginRoleVo.getUserId());
            sysUserRole.setRoleId(roleId);
//            保存
            sysUserRoleMapper.insert(sysUserRole);
        }

    }
}
